home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
SPACE 2
/
SPACE - Library 2 - Volume 1.iso
/
magazi~1
/
404
/
timers.c
< prev
next >
Wrap
C/C++ Source or Header
|
1989-04-11
|
3KB
|
107 lines
#include <stdio.h>
#include <osbind.h>
extern long timcnt; /* time counter */
extern int intr(); /* ISR reference */
main()
{
long t1, t2; /* time values */
int i; /* loop count */
char k; /* loop exit control character */
k = 0;
init_tmr(); /* initialize & start msec timer */
while( k != 'q') {
printf("Initial timcnt value is %D\n", timcnt);
wait_ms(100L); /* 100 msec delay */
printf("After first delay, timcnt = %D\n", timcnt);
t1 = timcnt; /* time marker #1 */
wait_sec(10L); /* 10 second delay */
wait_ms(250L); /* 250 msec delay */
calc_ms(t1,"since t1"); /* result should be 10250 msec */
t2 = timcnt; /* time marker #2 */
for(i=0; i<9000; i++) /* dummy code loop */
k = k;
calc_ms(t2,"since t2"); /* dummy code execution time */
calc_ms(t2, 0L); /* same call without id string */
printf("To quit, type a 'q', otherwise ");
printf("strike another key \n");
k = Bconin(2);
}
Jdisint(13); /* disable timer a interrupt:
required before exit!! */
}
init_tmr() /* Initialize Timer A */
{
timcnt = 0;
Jdisint(13); /* disable timer a interrupt */
Xbtimer(0x00, 0x04, 49, intr); /* set up timer a:
0x04 = prescale divide by 50
49 = count down value
intr = interrupt vector
(ISR address) */
Jenabint(13); /* enable timer a interrupt */
}
wait_ms(ms) /* Delay 'ms' milliseconds */
long ms;
{
long delta, t1;
delta = 0;
t1 = timcnt;
while(delta < ms) {
delta = timcnt - t1;
}
}
wait_sec(sec) /* Delay 'sec' seconds */
long sec;
{
long diff, t1;
diff = 0;
t1 = timcnt;
while(diff < sec) {
diff = (timcnt - t1) / 1000; /* convert to seconds */
}
}
calc_ms(t1, str) /* Calculate elapsed time in milliseconds */
long t1;
char *str;
{
long t2;
t2 = timcnt;
if(str)
printf("Elapsed time = %D msec %s\n", t2 - t1, str);
else
printf("Elapsed time = %D msec \n", t2 - t1);
return((int)(t2 - t1));
}